package com.hu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hu.common.common.Constants;
import com.hu.common.controller.BaseController;
import com.hu.common.pojo.Dict;
import com.hu.common.pojo.Menu;
import com.hu.common.pojo.dto.Response;
import com.hu.user.service.IMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hu
 * @since 2023-08-02
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Resource
    private IMenuService menuService;
//
//    @Resource
//    private DictMapper dictMapper;

    // 新增或者更新
    @PostMapping("/save")
    public Response save(@RequestBody Menu menu) {
        return success(menuService.saveOrUpdate(menu));
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        return success(menuService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        return success(menuService.removeByIds(ids));
    }

    @GetMapping
    public Response findAll(@RequestParam(defaultValue = "") String name) {
//        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("name",name);
//        queryWrapper.orderByDesc("id");
//        List<Menu> menuList = menuService.list(queryWrapper);
//        // 找出pid = null的一级菜单
//        List<Menu> ParentNodes = menuList.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
//        for (Menu menu : ParentNodes) {
//            menu.setChildren(menuList.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
//        }
        return success(menuService.findMenus(name));
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return success(menuService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.orderByDesc("id");
        return success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @GetMapping("/icons")
    public Response getIcons() {
//        QueryWrapper<Dict> queryMapper = new QueryWrapper<>();
//        queryMapper.eq("type", Constants.DICT_TYPE_ICON);
//        return success(dictMapper.selectList(null));


        return success(null);
    }
}
